import os
import time
from clickadmin.config import globalize_settings
from clickadmin.models import initialize_sqla
from pyramid.authentication import SessionAuthenticationPolicy
from pyramid.authorization import ACLAuthorizationPolicy
from pyramid.config import Configurator
from pyramid_beaker import (session_factory_from_settings, 
    set_cache_regions_from_settings)


def main(global_config, **settings):
    globalize_settings(settings)
    initialize_sqla(settings)
    set_cache_regions_from_settings(settings)
    config = Configurator(
        settings=settings,
        authentication_policy=SessionAuthenticationPolicy(),
        authorization_policy=ACLAuthorizationPolicy(),
        session_factory=session_factory_from_settings(settings))
    config.include('pyramid_celery')
    config.include('pyramid_jinja2')
    config.include('pyramid_mailer')
    config.include('pyramid_rewrite')
    config.include('pyramid_tm')
    config.include('clickadmin.config.routes')
    config.add_renderer('.html', 'pyramid_jinja2.renderer_factory')
    config.add_static_view(settings['static_url'], 'clickadmin:static')
    config.scan()
    os.environ['TZ'] = settings['timezone']
    time.tzset()
    return config.make_wsgi_app()